using FluentValidation;
using SageKing.Designer.Core.Models;

namespace SageKing.Studio.Pages.Dashboard.Components;
public class OutputModelValidator : AbstractValidator<OutputDefinitionModel>
{
    public OutputModelValidator(WorkflowDefinition workflowDefinition)
    {
        RuleFor(x => x.Name).NotEmpty().WithMessage("Please enter a name for the output.");
        
        RuleFor(x => x.Name)
            .Must((context, name, cancellationToken) =>
            {
                var existingOutput = workflowDefinition.Outputs.FirstOrDefault(x => x.Name == name);
                return existingOutput == null || existingOutput.Name == context.Name;
            })
            .WithMessage("An input with this name already exists.");
    }
}